package com.jt.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.OrderItemMapper;
import com.jt.mapper.OrderMapper;
import com.jt.mapper.OrderShippingMapper;
import com.jt.pojo.Order;
import com.jt.pojo.OrderItem;
import com.jt.pojo.OrderShipping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Cx
 * @stage JT Of Fourth stage
 * @creat 2020-12-17 14:48
 */
@Service(timeout = 3000)
public class OrderServiceImpl implements DubboOrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private OrderShippingMapper orderShippingMapper;

    @Override
    @Transactional
    public String saveOrder(Order order) {
        // 拼接字符串
        String orderId = "" + order.getUserId() + System.currentTimeMillis();
        // 实现订单入库操作
        order.setOrderId(orderId).setStatus(1); // 未付款状态
        orderMapper.insert(order);
        // 订单物流入库
        OrderShipping orderShipping = order.getOrderShipping();
        orderShipping.setOrderId(orderId);
        orderShippingMapper.insert(orderShipping);
        // 实现订单商品入库
        List<OrderItem> orderItems = order.getOrderItems();
        for (OrderItem orderItem : orderItems) {
            orderItem.setOrderId(orderId);
            orderItemMapper.insert(orderItem);
        }
        System.out.println("订单完成 ^ _ ^");
        return orderId;
    }

    @Override
    public Order findOrderById(String orderId) {
        // 查询订单数据
        Order order = orderMapper.selectById(orderId);
        // 查询订单物流信息
        OrderShipping orderShipping = orderShippingMapper.selectById(orderId);
        // 查询订单商品信息
        QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order", orderId);
        List<OrderItem> orderItems = orderItemMapper.selectList(queryWrapper);
        // 查询数据后封装返回
        order.setOrderItems(orderItems).setOrderShipping(orderShipping);
        return null;
    }

}
